﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Push.Entities;
using System.Windows.Threading;
using Push.EventArguments;
using System.Diagnostics;

namespace Push
{
    public partial class GamePage : PhoneApplicationPage
    {
        GameManager gameManager;

        bool IsSelectTrajectory = false;

        double Direction, Velocity;

        public GamePage()
        {
            InitializeComponent();

            gameManager = new GameManager();
            gameManager.DiscAdded += new GameManager.DiscAddedEventHandler(gameManager_DiscAdded);
            gameManager.NextTurn += new GameManager.NextTurnChangedEventHandler(gameManager_NextTurn);

            gameManager.GenerateDiscs(GameManager.DiscsCount, true);

            gameManager.DiscToStart();

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);            
        }

        void gameManager_DiscAdded(object sender, DiscAddedEventArgs e)
        {
            DiscsCanvas.Children.Add(e.disc);
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            gameManager.Update();
        }

        public void NewGame()
        {
            gameManager.NewGame();
        }

        private void DiscsCanvas_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
        }

        private void DiscsCanvas_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            if (gameManager.gameState == GameState.Pause)
            {
                if (gameManager.FirstPlayerCurrentDisc != null)
                {
                    if (e.ManipulationOrigin.Y > GameManager.TotalHeight - GameManager.DiscTopOffset * 4
                        && e.ManipulationOrigin.X > GameManager.TotalWidth / 2 - GameManager.DiscTopOffset * 2
                        && e.ManipulationOrigin.X < GameManager.TotalWidth / 2 + GameManager.DiscTopOffset * 2)
                    {
                        IsSelectTrajectory = true;

                        FirstPlayerTrajectory.Visibility = System.Windows.Visibility.Visible;
                        FirstPlayerTrajectory.Height = 0;

                        Direction = 0;
                        Velocity = 0;
                    }
                }
            }
        }

        private void DiscsCanvas_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            if (IsSelectTrajectory)
            {
                double dX = (e.ManipulationOrigin.X + e.DeltaManipulation.Translation.X - GameManager.TotalWidth / 2);
                double dY = Math.Max(GameManager.TotalHeight - GameManager.DiscTopOffset - (e.ManipulationOrigin.Y + e.DeltaManipulation.Translation.Y), 0);
                double length = Math.Sqrt(dX * dX + dY * dY);
                double limit = 100;
                double new_length = Math.Max(length - limit, limit);
                dX = dX / length * new_length;
                dY = dY / length * new_length;
                length = new_length;
                length = Math.Min(length, 600/*GameManager.TotalHeight / 2 - GameManager.DiscTopOffset*/);

                FirstPlayerTrajectory.Height = length;
                double A = 270 - Math.Atan2(dY, dX) * 180 / Math.PI;
                A = Math.Min(250, Math.Max(110, A));                
                (FirstPlayerTrajectory.RenderTransform as CompositeTransform).Rotation = A;

                Direction = A - 180;
                Velocity = length * 2;
            }
        }

        private void DiscsCanvas_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            IsSelectTrajectory = false;

            if (Velocity > 0)
            {
                gameManager.PushDisc((int)Direction, (int)Velocity);

                Direction = 0;
                Velocity = 0;

                FirstPlayerTrajectory.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void gameManager_NextTurn(object sender, EventArgs e)
        {

        }
    }
}